package com.leza.myapplication.ui.home;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.leza.myapplication.R;
import com.leza.myapplication.onenetapi.EntityData;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class LinearAdapter extends RecyclerView.Adapter<LinearAdapter.LinerViewHolder> {

    private final Context context;
    private List<EntityData> entityDataList;
    static private final List<DataType> dataTypeList = new ArrayList<>();
    static private final HashMap<String, DataType> dataDict = new HashMap<>();

    static  {
        String[]  devicesId = {"NULL", "temperature", "Light_data", "humidity", "UVI",
                "rs_light", "rain", "smoke", "soil_moisture",
                "flame", "soil_temperature"};

        dataTypeList.add(new DataType("暂无数据", R.mipmap.ic_launcher, ""));
        dataTypeList.add(new DataType("空气温度", R.mipmap.temperature, "℃"));
        dataTypeList.add(new DataType("光线强度", R.mipmap.light, "lx"));
        dataTypeList.add(new DataType("空气湿度", R.mipmap.humidity, "%"));
        dataTypeList.add(new DataType("紫外线强度", R.mipmap.uvi, "级"));
        dataTypeList.add(new DataType("485光线强度", R.mipmap.rs_light, "lx"));
        dataTypeList.add(new DataType("下雨检测", R.mipmap.rain, "bool"));
        dataTypeList.add(new DataType("可燃气检测", R.mipmap.smoke, "bool"));
        dataTypeList.add(new DataType("土壤湿度", R.mipmap.soil_moisture, "%"));
        dataTypeList.add(new DataType("火焰监测", R.mipmap.flame, "bool"));
        dataTypeList.add(new DataType("土壤温度", R.mipmap.soil_temperature, "℃"));

        for (int i = 0; i < devicesId.length; i++) {
            dataDict.put(devicesId[i], dataTypeList.get(i));
        }

    }

    public LinearAdapter(Context context, List<EntityData> entityDataList) {
        this.context = context;
        this.entityDataList = entityDataList;
    }

    public void setEntityDataList(List<EntityData> entityDataList) {
        this.entityDataList = entityDataList;
    }

    @NonNull
    @Override
    public LinearAdapter.LinerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new LinerViewHolder(LayoutInflater.from(context).inflate(R.layout.item, parent, false));
    }

    @SuppressLint({"SetTextI18n", "UseCompatLoadingForDrawables"})
    @Override
    public void onBindViewHolder(@NonNull LinerViewHolder holder, int position) {
//        holder.textView.setText(list.get(position) + "摄氏度");
        String devicesId = (String) entityDataList.get(position).id;
        DataType item = dataDict.get(devicesId);

        holder.imageViewIcon.setImageDrawable(context.getResources().getDrawable(item.imageId));
        holder.textViewStatus.setText(item.Name);
        if (!"bool".equals(item.unit)) {
            holder.textViewValue.setText(entityDataList.get(position).value + item.unit);
        } else {
            int value = (int)entityDataList.get(position).value;
            holder.textViewValue.setText(value != 0 ? "有" : "无");
        }

    }

    @Override
    public int getItemCount() {
        return entityDataList.size();
    }

    static class LinerViewHolder extends RecyclerView.ViewHolder {

        private final TextView textViewValue;
        private final TextView textViewStatus;
        private final ImageView imageViewIcon;

        public LinerViewHolder(@NonNull View itemView) {
            super(itemView);
            textViewValue = itemView.findViewById(R.id.temperature);
            textViewStatus = itemView.findViewById(R.id.item_status);
            imageViewIcon = itemView.findViewById(R.id.item_image);
        }
    }
}
